package com.itheima.infoManager.implDemo.manager.dao;

import com.itheima.infoManager.implDemo.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    private static ArrayList<Student> stus = new ArrayList<>();

    static {
        Student stu1 = new Student("heima001","张三","23","1998-88-99");
        Student stu2 = new Student("heima002","李四","24","2000-88-99");

        stus.add(stu1);
        stus.add(stu2);
    }

    //添加
    @Override
    public boolean addStudent(Student stu) {
       stus.add(stu);
       return true;
    }

    //返回数组——查看
    @Override
    public Student[] findAllStudent() {
        Student[] stus1 = new Student[stus.size()];
        for (int i = 0; i < stus1.length; i++) {
            stus1[i]=stus.get(i);
        }
        return stus1;
    }

    //删除
    @Override
    public boolean deleteStudent(String delId) {
        //调用getIndex方法，取得当前id在数组的索引位置
        int index = getIndex(delId);
        if (index==-1){
            return false;
        }else {
            stus.remove(index);
            return true;
        }
    }

    //根据id查找其学生对象所在数组索引处
    @Override
    public int getIndex(String id) {
        for (int i = 0; i < stus.size(); i++) {
            Student stu = stus.get(i);
            if (stu!=null&&stu.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    //修改
    @Override
    public boolean updateStudent(String upId, Student student) {
        //根据id查找其学生对象所在数组索引处
        int index = getIndex(upId);
        if (index==-1){
            return false;
        }else{
            stus.set(index,student);
            return true;
        }

    }
}
